/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package p2a;

import java.util.Random;
import java.util.Vector;

/**
 *
 * @author user
 */
public class EcpTest {

    class ECP {

        String value;
        boolean valid;

        public ECP(String value) {
            this.value = value;
            this.valid = true;
        }

        public ECP() {
        }
    }

    public Vector<TestCase> generateTestCasesFromRules(Vector<Rule> Rs) {
        Vector<TestCase> testCases = new Vector();
        
        for(int i=0; i< Rs.size(); i++)
        {
            testCases.addAll(generateTestCasesFromRule(Rs.get(i)));
        }
        return testCases;
    }
    public Vector<TestCase> generateTestCasesFromRule(Rule R) {
        Vector<TestCase> testCases = new Vector();
        for (int p = 0; p < 4; p++) {
            Vector<ECP> ecp = new Vector();
            if (p == 0) {
                ecp = this.IP(R.getSrcIp());
            }
            if (p == 1) {
                ecp = this.PORT(R.getSrcPort());
            }
            if (p == 2) {
                ecp = this.IP(R.getDestIp());
            } else if (p == 3) {
                ecp = this.PORT(R.getDestPort());
            }

            for (int idx = 0; idx < ecp.size(); idx++) {
                TestCase test_case = new TestCase();
                test_case.bind(R);
                if (p == 0) {
                    test_case.setSrcIp(ecp.get(idx).value);
                } else if (p == 1) {
                    test_case.setSrcPort(ecp.get(idx).value);
                } else if (p == 2) {
                    test_case.setDestIp(ecp.get(idx).value);
                } else if (p == 3) {
                    test_case.setDestPort(ecp.get(idx).value);
                }
                if (ecp.get(idx).valid == false) {
                    test_case.inverseAction();
                }
                testCases.add(test_case);
            }
        }
        echoTestCases(testCases);
        return testCases;
    }

    private void echoTestCases(Vector<TestCase> testCases) {
        for (int idx = 0; idx < testCases.size(); idx++) {
            System.out.println("TC:" + testCases.get(idx).toString());
        }
    }

    private Vector<ECP> IP(String ip) {
        Vector<ECP> ECs = new Vector();

        if (ip.equals("*.*.*.*") || ip.equalsIgnoreCase("any")) {
            String[] ECv = {"10.0.0.1", "172.16.0.1", "192.168.0.1", "127.0.0.1", "88.241.34.41", "215.15.168.23"};
            for (int idx = 0; idx < ECv.length; idx++) {
                ECP test = new ECP();
                test.valid = true;
                test.value = ECv[idx];
                ECs.add(test);
            }
        } else if (ip.indexOf("*") >= 0) {

            String masks[] = {"/8", "/16", "/24"};
            String mask = masks[3 - Helper.getCharCount(ip, '*')];
            String cidr = ip.replace("*", "0").concat(mask);
            String basecidr = cidr;
            int rep = 0;
            do {
                SubnetUtils su = new SubnetUtils(cidr);

                String[] ips = su.getInfo().getAllAddresses();
                ECP test1 = new ECP();
                test1.valid = true;
                test1.value = su.getInfo().getLowAddress();
                ECP test2 = new ECP();
                test1.valid = true;
                test2.value = su.getInfo().getHighAddress();
                ECP test3 = new ECP();
                test3.valid = true;
                //test3.value = // Helper.long2dotted(Helper.host2long(test1.value))+ ((Helper.host2long(test2.value) - Helper.host2long(test1.value)) / 2);
                test3.value = su.getInfo().getAllAddresses()[(su.getInfo().getAllAddresses().length / 2) - 1];
                ECs.add(test1);
                ECs.add(test2);
                ECs.add(test3);
                rep += 1;

                su = new SubnetUtils(basecidr);
                if (rep == 1) {
                    cidr = su.getInfo().getNextAddress().concat(mask);
                } else if (rep == 2) {
                    cidr = su.getInfo().getPreviousAddress().concat(mask);
                }

            } while (rep < 3);

        } else //host
        {
            //ECv is a host ip
            long longip = Helper.host2long(ip);
            Random r = new Random();
            String ECinv[] = {"1.0.0.1",
                Helper.long2dotted(longip - 1),
                Helper.long2dotted(longip + 1),
                Helper.long2dotted((long) (Helper.host2long("1.0.0.1") + ((longip - 1) - Helper.host2long("1.0.0.1")) * r.nextDouble())),
                Helper.long2dotted((long) ((longip + 1) + (Helper.host2long("255.255.255.254") - (longip + 1)) * r.nextDouble())),
                "255.255.255.254"};

            for (int idx = 0; idx < ECinv.length; idx++) {
                ECP test = new ECP();
                test.valid = false;
                test.value = ECinv[idx];
                ECs.add(test);
            }
            ECs.add(new ECP(ip));
        }
        return ECs;
    }

    private Vector<ECP> PORT(String port) {
        Vector<ECP> ECPs = new Vector();

        if (port.toUpperCase().equals("ANY")) {
            String[] ECv = {"0", "65535", "23", "80"};
            for (int idx = 0; idx < ECv.length; idx++) {
                ECP test = new ECP();
                test.valid = true;
                test.value = ECv[idx];
                ECPs.add(test);
            }
        } else {
            int ECvi[] = {Integer.parseInt(port) + 1, Integer.parseInt(port) - 1, Integer.parseInt(port)};
            for (int idx = 0; idx < ECvi.length; idx++) {
                ECP test = new ECP();
                test.value = String.valueOf(ECvi[idx]);
                if (idx == 2) {
                    test.valid = true;
                } else {
                    test.valid = false;
                }
                ECPs.add(test);
            }
        }
        return ECPs;
    }
}
